<div class="page">
	<div class="page-header">
		<div class="span8">
			<i class="icon-awesome-group icon-large"></i>
			<span class="divider"></span>
			<div class="title">
				<h3><?php echo __('Alunos');?></h3>
				<small><?php echo ('Lista de alunos cadastrados que podem solicitar empréstimo');?></small>
			</div>
		</div>
		<div class="span4">
			<div class="page-header-fields pull-right">
				<?php echo $this->Html->link('<i class="icon-awesome-plus icon-white"></i>'.__('Novo'), array('action' => 'add'), array('escape' => false, 'class' => 'btn btn-primary btn-square', 'id' => 'btn-add-aluno')); ?>
			</div>
		</div>
	</div>
	<div class="page-content">
		<?php echo $this->Form->create('Filter', array('class' => 'well')); ?>
		<div class="control-group">
			<div class="controls">
				<div class="input-append">
					<?php echo $this->Form->input('palavra_chave', array('label' => false, 'div' => false, 'class' => 'span12','placeholder' => 'Digite a palavra-chave')); ?>
					<?php echo $this->Form->button('<i class="icon-search icon-white"></i> Pesquisar', array('type' => 'submit', 'div' => false, 'class' => 'btn btn-primary', 'escape' => false)); ?>
				</div>
			</div>
		</div>
		<?php echo $this->Form->end();?>
		<table class="table table-bordered table-striped table-condensed">
		<thead>
		<tr>
			<th width="12%"><?php echo $this->Paginator->sort('matricula', 'Matrícula');?></th>
			<th><?php echo $this->Paginator->sort('nome', 'Nome');?></th>
			<th width="30%"><?php echo $this->Paginator->sort('curso', 'Curso');?></th>		
			<th class="actions" width="15%"><?php echo __('Ações');?></th>
		</tr>
		</thead>
		<tbody>
<?php 
		foreach ($alunos as $aluno): ?>
		<tr>
			<td><?php echo h($aluno['Aluno']['matricula']); ?>&nbsp;</td>
			<td><?php echo h($aluno['Aluno']['nome']); ?>&nbsp;</td>
			<td><?php echo h($aluno['Curso']['nome']); ?>&nbsp;</td>			
			<td class="actions">
				<?php echo $this->Html->link('<i class="icon-eye-open icon-white"></i>', array('action' => 'view', $aluno['Aluno']['id']), array('escape' => false, 'class' => 'btn btn-primary btn-mini', 'id' => 'btn-view-aluno', 'title' => 'Visualizar Aluno')); ?>
				<?php echo $this->Html->link('<i class="icon-pencil icon-white"></i>', array('action' => 'edit', $aluno['Aluno']['id']), array('escape' => false, 'class' => 'btn btn-primary btn-mini', 'id' => 'btn-edit-aluno', 'title' => 'Editar Aluno')); ?>
				<?php echo $this->Form->postLink('<i class="icon-trash icon-white"></i>', array('action' => 'delete', $aluno['Aluno']['id']), array('escape' => false, 'class' => 'btn btn-danger btn-mini', 'id' => 'btn-delete-aluno'), __('Are you sure you want to delete # %s?', $aluno['Aluno']['id'])); ?>
			</td>
		</tr>
<?php endforeach; ?>
		</tbody>
		</table>
		
		<div class="pull-left">
			<?php echo $this->Paginator->counter(array('format' => __('Página {:page} de {:pages}, exibindo {:current} do total de {:count} registros'))); ?>
		</div>
		<div class="pagination pagination-right pagination-small text-right">
			<ul>
			<?php
				echo $this->Paginator->first(('<i class="icon-awesome-step-backward"></i>'), array('tag' => 'li', 'escape' => false));
				if ($this->Paginator->hasPrev()) {
					echo $this->Paginator->prev(('<i class="icon-awesome-backward"></i>'), array('tag' => 'li', 'escape' => false), null, array('class' => 'disabled', 'tag' => 'li', 'escape' => false, 'disabledTag' => 'a'));
				}
				echo $this->Paginator->numbers(array('separator' => '', 'tag' => 'li', 'currentClass' => 'active', 'currentTag' => 'a'));
				if ($this->Paginator->hasNext()) {
					echo $this->Paginator->next(('<i class="icon-awesome-forward"></i>'), array('tag' => 'li', 'escape' => false), null, array('class' => 'disabled', 'tag' => 'li', 'escape' => false, 'disabledTag' => 'a'));
				}
				echo $this->Paginator->last(('<i class="icon-awesome-step-forward"></i>'), array('tag' => 'li', 'escape' => false));
			?>
			</ul>
		</div>
		
	</div>
</div>


<div id="modal-form-aluno" class="modal hide fade">
	<div class="modal-header">
		<?php echo $this->Form->button('×', array('type' => 'button', 'div' => false, 'class' => 'close', 'data-dismiss' => 'modal')); ?>
		<h4>Cadastrar Novo Solicitante</h4>
	</div>
	<div class="modal-body">
	</div>
	<div class="modal-footer">
		<?php echo $this->Form->button('<i class="icon-awesome-remove icon-white"></i>&nbsp;<span>' . __('Cancelar') . '</span>', array('div' => false, 'class' => 'btn', 'data-dismiss' => 'modal'));?>		
		<?php echo $this->Form->button('<i class="icon-awesome-save icon-white"></i>&nbsp;<span>' . __('Salvar') . '</span>', array('div' => false, 'class' => 'btn btn-primary', 'id' => 'btn-save-aluno')); 	?>
	</div>
</div>

<script type="text/javascript">
	$(document).ready(function () {

		function displayModal(url) {
			$("#modal-form-aluno .modal-body").empty();
            $.get(url, function (data) {
            	$("#modal-form-aluno .modal-body").html(data);
            	$("#modal-form-aluno").modal();  
            })
            .always(function(){
            	$("#ajax-indicator").hide();
            });
		}
		
		$('.actions a.btn-primary').click(function(event) {
			event.preventDefault();
			
			var url = $(this).attr('href');
			var title = $(this).attr('title');
			var container = $('#modal-form-aluno');
			var header = container.children('.modal-header');
			var content = container.children('.modal-body');
			var footer = container.children('.modal-footer');
			
			$("#ajax-indicator").show();
			content.empty();
            $.get(url, function (data) {
            	header.children('h4:first').text(title);
				if (url.indexOf('add') >= 0) {
					footer.children('button:last-child').show();
				} else if (url.indexOf('edit') >= 0) {
					footer.children('button:last-child').show();
				} else if (url.indexOf('view') >= 0) {
					footer.children('button:last-child').hide();
				}
				content.html(data);
                container.modal();  
            })
            .always(function(){
            	$("#ajax-indicator").hide();
            });
            
		});
		
		$('#btn-save-aluno').click(function(event){
			event.preventDefault();
			var button = $(this);
			var copyButton = button.clone(true);
			var content =  $("#modal-form-aluno .modal-body");
			var form = content.children('form');
			
			$("#ajax-indicator").show();
			button.addClass('disabled').children('span').text('Salvando...').attr({disabled: 'disabled'});
		    $.post(form.attr('action'), form.serialize(), function (response) {
		        if ($(response).find('.error').length) {
		        	content.html(response);
		        	button.replaceWith(copyButton);
		        } else {
		           location.reload();
		        }
		    })
		    .always(function(){
		    	$("#ajax-indicator").hide();
		    });
		});
		
	});
</script>